package com.zhongshengnetwork.rightbe.game.activity;

import android.app.Activity;
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dd.CircularProgressButton;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.pulluprefresh.xlistview.XListView;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HistorySubjectModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HomeDataModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class HistorySubjectActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private TopBarView topbar;
    private List<HistorySubjectModel> list;
    private int pageindex = 0;
    private static final int pagerecords = 20;
    private boolean isHasMore = false;
    private XListView listview;
    private MyAdapter adapter;
    private String appid;
    private String subjectName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_subject);
        appid = getIntent().getStringExtra("appid");
        subjectName = getIntent().getStringExtra("subjectName");
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getTitleView().setText(subjectName);
        listview=(XListView) findViewById(R.id.history_subject_listview);
        list = new ArrayList<HistorySubjectModel>();
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        //这里一定不能漏掉
        listview.setPullLoadEnable(true);
        listview.setPullRefreshEnable(false);
        listview.setXListViewListener(new XListView.IXListViewListener() {

            @Override
            public void onRefresh() {
                //adapter.notifyDataSetChanged();
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                listview.setRefreshTime(sdf.format(date));
                pageindex = 0;
                getData();
            }

            @Override
            public void onLoadMore() {
                getData();
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("TAG","点击了："+position);
                int index = position - 1;
                while (index < 0){//解决部分手机点击第一条不是从1开始的问题
                    index+=1;
                }
                HistorySubjectModel historySubjectModel = list.get(index);
                go2Subject(historySubjectModel,"第 "+position+" 题");
            }
        });
        listview.startRefresh();
    }

    private void getData(){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.pageindexKey, pageindex+"");
        map.put(APIKey.pagerecordsKey,pagerecords+"");
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        map.put("appid",appid);
        String url = "miniapp/"+appid+"/gethistory.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                if (pageindex == 0){
                    listview.stopRefresh(true);
                }else {
                    listview.stopLoadMore();
                }
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    List<HistorySubjectModel> l = GsonTools.getHistorySubjectModel(data);
                    if (pageindex == 0){
                        list.clear();
                    }
                    if (l != null && l.size() > 0){
                        list.addAll(l);
                        pageindex += 1;
                    }
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(String msg) {
                if (pageindex == 0){
                    listview.stopRefresh(true);
                }else {
                    listview.stopLoadMore();
                }
            }
        });
    }

    private void go2Subject(HistorySubjectModel historySubjectModel, String indexStr){
        HomeDataModel homeDataModel = new HomeDataModel();
        homeDataModel.setAppid(historySubjectModel.getAppid());
        homeDataModel.setName(subjectName);
        String subjectIndex = "历史答题："+indexStr;
        if (historySubjectModel.getAppid().contains("subject")) {//综合题库
            Intent intent = new Intent(HistorySubjectActivity.this, SubjectWordActivity.class);
            intent.putExtra("model",homeDataModel);
            intent.putExtra("subjectIndex",subjectIndex);
            intent.putExtra("historySubjectModel",historySubjectModel);
            startActivity(intent);
        }else if (historySubjectModel.getAppid().equals("appid")){//成语猜猜
            Intent intent = new Intent(HistorySubjectActivity.this, GuessWordActivity.class);
            intent.putExtra("model",homeDataModel);
            intent.putExtra("subjectIndex",subjectIndex);
            intent.putExtra("historySubjectModel",historySubjectModel);
            startActivity(intent);
        }else if (historySubjectModel.getAppid().equals("linkwordappid")){//成语接龙
            Intent intent = new Intent(HistorySubjectActivity.this, LinkWordActivity.class);
            intent.putExtra("model",homeDataModel);
            intent.putExtra("subjectIndex",subjectIndex);
            intent.putExtra("historySubjectModel",historySubjectModel);
            startActivity(intent);
        }else if (historySubjectModel.getAppid().equals("smartwordappid")){//智多多
            Intent intent = new Intent(HistorySubjectActivity.this, HeaderWordActivity.class);
            intent.putExtra("model",homeDataModel);
            intent.putExtra("subjectIndex",subjectIndex);
            intent.putExtra("historySubjectModel",historySubjectModel);
            startActivity(intent);
        }else if(historySubjectModel.getAppid().contains("picappid")){//图片类型题目
            Intent intent = new Intent(HistorySubjectActivity.this, PicGameActivity.class);
            intent.putExtra("model",homeDataModel);
            intent.putExtra("subjectIndex",subjectIndex);
            intent.putExtra("historySubjectModel",historySubjectModel);
            startActivity(intent);
        }
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            HistorySubjectModel model = list.get(position);
            View view=convertView;
            view= LayoutInflater.from(HistorySubjectActivity.this).inflate(R.layout.history_subject_listview_item, null);
            TextView history_subject_listview_item_text = (TextView) view.findViewById(R.id.history_subject_listview_item_text);
            String text = (position+1)+". "+model.getAnswer();
            history_subject_listview_item_text.setText(text);
            return view;
        }

    }

}
